<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="车型管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="600px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
            <el-form-item label="车型名称" prop="vehicleTypeName">
                <el-input v-model="formData.vehicleTypeName"/>
            </el-form-item>
            <el-form-item label="座位数" prop="seatNum">
                <el-input-number v-model="formData.seatNum"/>
            </el-form-item>
            <el-form-item label="免费携带儿童数" prop="freeChildrenNum">
                <el-input-number v-model="formData.freeChildrenNum"/>
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark"/>
            </el-form-item>

            <el-form-item label="排序" prop="sort">
                <el-input-number v-model="formData.sort"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {VehicleTypeApi} from '@/api/user/VehicleTypeApi'
import {Common} from '@/utils/common'

export default {
    name: 'VehicleTypeUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                vehicleTypeName: '',
                seatNum: 0,
                remark: '',
                sort: 0
            },
            rules: {
                vehicleTypeName: [
                    {required: true, message: '请输入车型名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ],
                seatNum: [
                    {required: true, message: '请输入总座位数', trigger: 'blur'}
                ],
                sort: [
                    {required: true, message: '请输入排序', trigger: 'blur'}
                ]
            },
            dialogVisible: false
        }
    },
    methods: {

        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            if (data.id > 0) {
                this.$nextTick(() => {
                    this.formData = Common.assign(this.formData, data)
                })
            } else {
                this.formData.id = 0
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }
            }
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        VehicleTypeApi.update(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        VehicleTypeApi.insert(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>
